<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/*设备管理管理*/
class Equipment extends AdminBase{
	protected $client='';
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();
		$this->client=new \app\api\controller\JpusHelper();  
    }
	/*私教设备列表*/
	function index(){
		echo $this->fetch();
	}	
	//删除
	function delete(){
			$id=input("id");
			db("Equipment")->where("id=$id")->fetchsql(FALSE)->delete();
			$this->ajax_success("成功");
	}
	
	function add(){
		if(request()->isPost()){
			  $mac_addr=input("mac_addr","");
			   $count=db("Equipment")->where("mac_addr='$mac_addr'")->count(1);
			   if($count>0){
			   	$this->ajax_error("mac_addr 已经存在");
			   }
				db("Equipment")->insert(array(
					"mac_addr"=>input('mac_addr'),
					"name"=>input("name"),
					"descs"=>input("descs"),
					"shop_id"=>input("shop_id"),
				));			
			$this->ajax_success("成功");	
		}else{
			$this->assign("shops",db("Shop")->field("id,shop_name")->order("id desc")->select());
			echo $this->fetch();	
		}
	}
	/*推送*/
	function update(){
		if(!empty(input("version"))){
			$ids=input("ids");	
			db("Equipment")->where("id in ($ids)")->setField(array("version"=>input("version")));
			$alias=db("Equipment")->where(" id in ($ids)")->column("mac_addr");
			$result='';
			foreach ($alias as $key => $value) {
				$result[]=str_replace(array(":"), array(""),$value);
			}
			//私教列表
			$this->client->send_notice($result,input("version"),input("addrs"));			
			$this->ajax_success("成功");	
		}else{
			$ids=input("ids");	
			$this->assign("ids",$ids);
			echo $this->fetch();	
		}
	}
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10001008");
		$this->assign("menus",$auth_group);
	}
	
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
					 'mac_addr'=>array("field" =>"mac_addr","operator" =>"like"),
					 'name'=>array("field" =>"name","operator" =>"like"),
					 );
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$total=db("Equipment")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("Equipment")->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
			
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
	
}
